package com.stork.sms.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.stork.sms.R;
import com.stork.sms.fragment.LoginPageFragment;
import com.stork.sms.service.all.ServiceMethods;

public class ChangePasswordActivity extends Activity {
	
	
	EditText newpasswordtext,retypepasswordtext,usernametext;
	Button sendchangepasswordbutton;
	String newpassword,retypepassword,updatepasswordresult,username;
	TextView changepassworderrortext;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		usernametext=(EditText) findViewById(R.id.usernamepasswordchangeactivity);
		newpasswordtext=(EditText) findViewById(R.id.enterpassword);
		changepassworderrortext=(TextView) findViewById(R.id.seterrortext);
		retypepasswordtext=(EditText) findViewById(R.id.enterretypepassword);
		sendchangepasswordbutton=(Button) findViewById(R.id.sendchangepassword);
		sendchangepasswordbutton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				username=usernametext.getText().toString();
				newpassword=newpasswordtext.getText().toString();
				retypepassword=retypepasswordtext.getText().toString();
				if(username.equals(""))
				{
				usernametext.setError("* Enter user name");
				usernametext.requestFocus();
				}
				else if(newpassword.equals(""))
				{
				newpasswordtext.setError("* Enter New Password");
				newpasswordtext.requestFocus();
				}
				else if(retypepassword.equals(""))
				{
				retypepasswordtext.setError("* Enter Re-Type Password");
				retypepasswordtext.requestFocus();
				}
				else if(newpassword.equals("")&&retypepassword.equals(""))
				{
					 newpasswordtext.setError("* Enter new password");
					 newpasswordtext.requestFocus();
				}
					 else if(!newpassword.equals("")&&retypepassword.equals(""))
					 {
						 
						 retypepasswordtext.setError("* Enter Re-Type Password");
							retypepasswordtext.requestFocus();
				
				}
				 else if(newpassword.equals(retypepassword)){
					 new getchangepassword().execute();	 
				 }
				 else
				 {
					 newpasswordtext.setText("");
					 retypepasswordtext.setText("");
					 newpasswordtext.setError("* Enter Correct Password");
					 newpasswordtext.requestFocus();
					 retypepasswordtext.requestFocus();
					 Toast.makeText(ChangePasswordActivity.this,"PassWord Is Incorrect", Toast.LENGTH_LONG).show();
				 }
			}
		});
	}
	
public class getchangepassword extends AsyncTask<Void, Void, Void>
	{
	

		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			ServiceMethods sm=new ServiceMethods();
			
			
			
				
				//newpassword=newpasswordtext.getText().toString();
				
				
				updatepasswordresult=sm.UpdatePassword(username, newpassword);
				System.out.println("pdatepassword"+updatepasswordresult);
				
			
			
			
			
			if(updatepasswordresult.equals("111"))
			{
				
				Intent i=new Intent(getApplicationContext(),LoginPageFragment.class);
				startActivity(i);
				
			}
			else if(updatepasswordresult.equals("444"))
			{
				ChangePasswordActivity.this.runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				Log.i("welcome to cms","NO DATA found");
				Toast.makeText(ChangePasswordActivity.this, "Invalid Valid User ", Toast.LENGTH_SHORT).show();
				usernametext.setText("");
				changepassworderrortext.setVisibility(View.VISIBLE);
				
					}
					});
			}
			else if(updatepasswordresult.equals("404"))
			{
				ChangePasswordActivity.this.runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				Log.i("welcome to cms","NO DATA found");
				Toast.makeText(ChangePasswordActivity.this, "Server Access Denied ", Toast.LENGTH_SHORT).show();
					}
					});
			}
			else if(updatepasswordresult.equals("000"))
			{
				ChangePasswordActivity.this.runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				Log.i("welcome to cms","NO DATA found");
				Toast.makeText(ChangePasswordActivity.this, "Service is Not Runninig ", Toast.LENGTH_SHORT).show();
					}
					});
			}
			
			
		
			
			
			return null;
		}
	}
}

